﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.AspNet.SignalR;
using Microsoft.AspNet.SignalR.Hubs;

namespace Sustainalytics.DW.Synchronization
{
    [HubName("syncHub")]
    public class SyncHub : Hub
    {
        /// <summary>
        /// Context instance to access client connections to broadcast to
        /// </summary>
        public static IHubContext HubContext
        {
            get 
            {
                return GlobalHost.ConnectionManager.GetHubContext<SyncHub>();
            }
        }
        
        public void StartSync(bool isSyncAutomatic)
        {
            Clients.All.addMessage(string.Format("{0} Synchronizing...", DateTime.Now.ToString("HH:mm:ss")));

            Globals.DwSync.StartManualSync();
        }

        public void Send(string message)
        {
            Clients.All.addMessage(message);
        }

        public override Task OnConnected()
        {
            Clients.All.isSyncAutomatic(true);

            return base.OnConnected();
        }

        public override Task OnReconnected()
        {
            Clients.All.isSyncAutomatic(true);

            return base.OnReconnected();
        }

    }
}
